import json

from flask import Blueprint, request
from .gen import generate_start_init, generate_start_un_init
from app.admin.models import TModules

bp = Blueprint(name='write', import_name=__name__, url_prefix='/write')


@bp.route('/generate', methods=['POST'])
def generate():
    params = json.loads(request.data.decode('utf-8'))
    dict_params = dict(params)
    module = TModules.query.filter_by(module_code=dict_params['label_id']).first()
    if module.init_load == 1:
        return generate_start_init(dict_params)
    return generate_start_un_init(dict_params, module)
